package com.eduction.modules.system.dao;

import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.eduction.common.dbutil.DbHelper;
import com.eduction.modules.system.entity.InterviewAnalyse;

@Repository
public class InterviewAnalyseRepository {

	@Autowired
	private DbHelper dbhelper;
	@Autowired
	private InterviewAnalyseDao interviewAnalyseDao;
	/**
	 * 保存表格
	 */
	public String saveInterviewAnalyse(InterviewAnalyse i){
		this.interviewAnalyseDao.save(i);
		return i.getPid();
	}
	/**
	 * 查看某个表格信息
	 */
	public InterviewAnalyse queryInterviewAnalyseById(String id,String eid,String sid) throws SQLException{
		String sql = "select a.*,s.s_name as schoolName,e.s_name as expertName from t_interview_analyse as a left join t_school as s "
				+" on a.school_id = s.p_id left join t_expert as e on a.expert_id = e.p_id where a.sign=? and a.expert_id = ? and a.school_id = ?";
		return this.dbhelper.getBean(sql, InterviewAnalyse.class, id,eid,sid);
	}
	/**
	 * 查看某个表格信息
	 */
	public InterviewAnalyse queryInterviewAnalyseById(String id,String sid) throws SQLException{
		String sql = "select a.*,s.s_name as schoolName,e.s_name as expertName from t_interview_analyse as a left join t_school as s "
				+" on a.school_id = s.p_id left join t_expert as e on a.expert_id = e.p_id where a.sign=? and a.school_id = ?";
		return this.dbhelper.getBean(sql, InterviewAnalyse.class, id,sid);
	}
	
	/**
	 * 查询这个专家创建的所有表格信息
	 */
	public List<InterviewAnalyse> queryIAsByExpert(String expertId) throws SQLException{
		String sql = "SELECT a.p_id as pid,s.s_name as schoolName,a.school_id as schoolId,a.sign as sign from t_interview_analyse as a LEFT JOIN t_school as s on a.school_id = s.p_id where a.expert_id = ?";
		return this.dbhelper.getBeanList(sql, InterviewAnalyse.class, expertId);
	}

	/**
	 * 根据Id查询表格信息
	 * @param pId
	 * @return
	 */
    public InterviewAnalyse findById(String pId) {
    	return interviewAnalyseDao.findOne(pId);
    }

    public void deleteByPid(String pid) {
		interviewAnalyseDao.delete(pid);
    }

	public void deleteInterviewAnalyseMapState(String sign, String schoolId, String expertId)  throws SQLException{
		String sql = "delete from t_course_analyse where sign =? and school_id=? and expert_id=? ";
		this.dbhelper.execute(sql,sign, schoolId, expertId);
    }
}
